﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Text;
using System.Transactions;

using Toenda.Foundation.Data;

namespace Toenda.Foundation.Messaging {
	/// <summary>
	/// Provides the message object for the <see cref="Toenda.Foundation.Messaging.MessageManager" />. 
	/// It is also a base for the specific classes <see cref="SystemMessage" /> and <see cref="EmailMessage" />.
	/// </summary>
	public abstract class Message {
		#region "Data Object"

		/// <summary>
		/// Gets or sets the id.
		/// </summary>
		/// <value>The id.</value>
		public long Id { get; private set; }

		/// <summary>
		/// Gets or sets the timestamp.
		/// </summary>
		/// <value>The timestamp.</value>
		public DateTime Timestamp { get; set; }

		/// <summary>
		/// Gets or sets the title.
		/// </summary>
		/// <value>The title.</value>
		public string Title { get; set; }

		/// <summary>
		/// Gets or sets the subject.
		/// </summary>
		/// <value>The subject.</value>
		public string Subject { get; set; }

		/// <summary>
		/// Gets or sets the text.
		/// </summary>
		/// <value>The text.</value>
		public string Text { get; set; }

		/// <summary>
		/// Gets or sets a value indicating whether this instance is read.
		/// </summary>
		/// <value><c>true</c> if this instance is read; otherwise, <c>false</c>.</value>
		public bool IsRead { get; set; }

		/// <summary>
		/// Gets or sets a value indicating whether [sort by sender].
		/// </summary>
		/// <value><c>true</c> if [sort by sender]; otherwise, <c>false</c>.</value>
		public bool SortBySender { get; set; }

		/// <summary>
		/// Initializes a new instance of the <see cref="Message"/> class.
		/// </summary>
		public Message() {
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="Message"/> class.
		/// </summary>
		/// <param name="record"></param>
		public Message(IDataRecord record) {
			if(record == null) {
				throw new ArgumentNullException("record");
			}

			this.Id = (long)record["id"];
			this.Timestamp = (DateTime)record["timestamp"];
			this.Title = record["title"].ToString();
			this.Subject = record["subject"].ToString();
			this.Text = record["text"].ToString();
			this.IsRead = (bool)record["isRead"];
		}
		
		#endregion
	}
}
